<?php


namespace App\Controller\Test;


use App\Controller\BaseController;
use App\Utils\MongoDBClient;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Controller]
class MongoController extends BaseController
{
    #[RequestMapping(path: "insert", methods: "POST")]
    public function insert()
    {
        $client = MongoDBClient::getInstance();
        $insertData = array();
        $insertData['uid'] = (int)$this->request->post('id', 1);
        $insertData['user_id'] = 4;
        $insertData['type'] = 4;
        $insertData['content'] = $this->request->post('content', '送我点礼物嘛');
        $insertData['extend'] = 7;
        $insertData['phone'] = $this->request->post('phone', '15773151949');
        $insertData['url'] = 0;
        $insertData['ctime'] = time();
        $insertData['from'] = 1;
        $insertData['version_code'] = 101;
        $insertData['channel'] = 'dev';
        $insertData['check_result'] = 0;
        $insertData['from_page'] = 2;

        $tt = $client->insert('tb_tipoff', $insertData);

        return [
            'tt' => $tt
        ];
    }

    #[RequestMapping(path: "query", methods: "POST")]
    public function query()
    {
        $client = MongoDBClient::getInstance();
        $where['uid'] = ['$gt' => intval($this->request->post('uid', 1))];
        // $where['look'] = ['$in' => [1, 2, 3]];
        $result = $client->find('tb_tipoff', $where, [
            'sort' => ['uid' => 1],// -1降序,1升序
            'limit' => 50,
        ]);
        return [
            'data' => $result
        ];
    }

    #[RequestMapping(path: "update", methods: "POST")]
    public function update()
    {
        $client = MongoDBClient::getInstance();
        // = 查询
        $where['uid'] = intval($this->request->post('uid', 1));
        // in 查询
        // $where['from_page'] = ['$in' => $this->request->post('from_page', [1])];

        // $inc 自增
        // $update['$inc'] = [
        //     'uid' => 1,
        // ];
        // $set 设置更新
        $update['$set'] = [
            'content' => $this->request->post('content', '送我点礼物嘛')
        ];
        $tt = $client->update('tb_tipoff', $where, $update, false, true);
        return [
            'tt' => $tt,
        ];
    }

    #[RequestMapping(path: "delete", methods: "POST")]
    public function delete()
    {
        $client = MongoDBClient::getInstance();
        $where['uid'] = intval($this->request->post('uid', 1));
        $tt = $client->delete('tb_tipoff', $where);
        return [
            'tt' => $tt,
        ];
    }


}